#include <stdio.h>
#include <pmsis.h>
#include "testbench.h"
#include "quiddikey.h"

#if defined(__PLATFORM_RTL__)
char ac[GAP_QUIDDIKEY_AC_BYTES] = {0x6, 0x3, 0x1, 0x0, 0x0, 0x13, 0x2, 0x20, 0x9f, 0xd6, 0x58, 0x2a, 0xd9, 0x70, 0x36, 0x74, 0x78, 0xf1, 0xf2, 0x59, 0xf0, 0xfb, 0x45, 0x42, 0x48, 0xf8, 0xda, 0xab, 0xb8, 0x87, 0x7e, 0x47, 0x8f, 0x6f, 0x2d, 0xc5, 0x76, 0x66, 0x7f, 0xc1, 0x20, 0xa6, 0xb9, 0xed, 0xfa, 0xab, 0x9, 0x52, 0xf3, 0x36, 0xf6
                                   , 0xa1, 0x8, 0x70, 0xda, 0xbe, 0x1b, 0x48, 0xc3, 0x1d, 0x21, 0x94, 0x70, 0xe9, 0x3c, 0xea, 0x9c, 0x5f, 0x17, 0x62, 0x8f, 0xbd, 0x5a, 0xc, 0xe1, 0x2b, 0x3e, 0x56, 0xe9, 0x56, 0xd5, 0x38, 0x80, 0x79, 0x57, 0xbb, 0xb3, 0x29, 0x15, 0x40, 0x4d, 0xa9, 0x38, 0xdd, 0xbc, 0x2d, 0x23, 0xf4, 0x3, 0x59, 0x23
                                   , 0x22, 0x67, 0xd2, 0xa, 0x7d, 0xf0, 0x2a, 0x5b, 0xfc, 0xa4, 0xfd, 0x61, 0x23, 0x82, 0x8f, 0x79, 0x5f, 0xfc, 0xff, 0x1f, 0x79, 0x8, 0x86, 0x9, 0x31, 0x9f, 0xf0, 0x3c, 0x34, 0xdc, 0xeb, 0x92, 0xaf, 0x10, 0x8f, 0xf8, 0x38, 0x15, 0xa1, 0x4, 0x88, 0x6a, 0xda, 0x1, 0x3c, 0xed, 0xa2, 0x77, 0x4c, 0x44
                                   , 0xe7, 0x45, 0x3, 0x36, 0x85, 0x4b, 0xf, 0x6f, 0xcd, 0x70, 0xd7, 0x4f, 0x38, 0xc1, 0x6c, 0x96, 0x3a, 0x49, 0xda, 0xe8, 0xec, 0x3a, 0xd2, 0xc5, 0x50, 0xc8, 0x79, 0x6c, 0xe5, 0xa7, 0x6c, 0xc0, 0x87, 0xa1, 0xde, 0xde, 0x4e, 0x45, 0x11, 0x6e, 0x1, 0x48, 0x92, 0x5c, 0xa9, 0xed, 0xbc, 0x86, 0xe9, 0x38
                                   , 0x21, 0x5e, 0x71, 0xd, 0x9a, 0x98, 0x2f, 0xa0, 0x4a, 0x4b, 0x50, 0xa2, 0xbd, 0xfc, 0x55, 0xec, 0x61, 0xe4, 0x96, 0x14, 0xa9, 0x2e, 0x41, 0xc7, 0xc6, 0x18, 0x5b, 0x5e, 0x10, 0xc8, 0x54, 0x28, 0xdd, 0xa, 0x6f, 0x80, 0xff, 0xc7, 0x72, 0x24, 0x6f, 0x5c, 0x56, 0xdb, 0xb3, 0xe4, 0x1e, 0xa, 0x24, 0xed
                                   , 0x70, 0x54, 0x0, 0xf7, 0x30, 0xcc, 0x91, 0x40, 0x2, 0x6f, 0x99, 0xdb, 0x11, 0xc5, 0x58, 0x7b, 0x53, 0x5d, 0xe2, 0x1b, 0x7f, 0x45, 0x12, 0xd5, 0x5d, 0x57, 0xbc, 0x65, 0xb7, 0xb4, 0x49, 0xf8, 0xf, 0x8a, 0xf5, 0xf6, 0x47, 0x52, 0x37, 0x3f, 0xda, 0x7, 0xb0, 0x21, 0x7e, 0xa9, 0xe3, 0x80, 0xc6, 0x77
                                   , 0xc3, 0x3f, 0xfb, 0x9d, 0xe7, 0x3a, 0x1b, 0x30, 0xdb, 0x3f, 0xe1, 0xbd, 0x74, 0xba, 0x2b, 0xc1, 0x9b, 0x3a, 0xcb, 0x4, 0xae, 0xdf, 0xfb, 0x2b, 0xcb, 0x41, 0x52, 0xc7, 0x9d, 0x1d, 0x97, 0x32, 0xab, 0x6b, 0x35, 0xdf, 0x0, 0x3d, 0x9b, 0x62, 0x1a, 0x1d, 0x2a, 0x54, 0x35, 0xbc, 0x8, 0x2d, 0x9, 0x0
                                   , 0x64, 0x57, 0x5a, 0x10, 0x48, 0xf1, 0xb8, 0xe8, 0x91, 0xa3, 0x2f, 0x69, 0xb4, 0x35, 0x86, 0x7d, 0x6c, 0x35, 0x45, 0xb1, 0xa6, 0x15, 0xbb, 0x83, 0xfe, 0xe2, 0xc0, 0x1f, 0xac, 0xc9, 0x2b, 0x16, 0xe8, 0xda, 0xb3, 0x43, 0x1, 0x71, 0x3b, 0xfa, 0x33, 0x19, 0x42, 0x46, 0x30, 0xb, 0xd3, 0x95, 0x56, 0xe4
                                   , 0xa7, 0x5c, 0x22, 0xc8, 0x8, 0xad, 0xf1, 0xe4, 0x24, 0xb6, 0x1c, 0x9f, 0x5c, 0xb3, 0x69, 0x27, 0x7f, 0xbf, 0x3e, 0x8c, 0xa9, 0xb4, 0x3d, 0xcb, 0x38, 0xbf, 0xf5, 0x54, 0xce, 0x53, 0xdd, 0x85, 0x77, 0x4d, 0x82, 0xc5, 0x43, 0x1c, 0x1d, 0xb2, 0xd3, 0xc6, 0x1e, 0xe, 0xd9, 0x5d, 0xd, 0xa7, 0x1e, 0xd7
                                   , 0x9, 0x7d, 0x15, 0x87, 0x18, 0xa0, 0x31, 0x13, 0xa9, 0x97, 0xad, 0x17, 0x52, 0x15, 0x69, 0x75, 0x93, 0x10, 0x6c, 0x17, 0xb2, 0x8a, 0xff, 0xa2, 0x6c, 0xb6, 0xb5, 0xbc, 0x12, 0x9, 0x2a, 0x12, 0x1c, 0xe6, 0x66, 0x8d, 0x47, 0x30, 0x2f, 0x71, 0xf2, 0x6b, 0x3c, 0x7b, 0x21, 0x1f, 0xf1, 0x1b, 0x6f, 0x30
                                   , 0x9f, 0x3, 0x50, 0xb0, 0xa0, 0x77, 0x35, 0x9b, 0xde, 0x29, 0x9a, 0xaf, 0xcc, 0xcd, 0xca, 0x42, 0x98, 0x4, 0x70, 0x3f, 0x43, 0x2, 0x8f, 0xf2, 0x25, 0x5b, 0xad, 0xed, 0x66, 0xb7, 0xf7, 0xf1, 0x24, 0x75, 0x73, 0x67, 0x7c, 0x19, 0xaf, 0xd3, 0x3f, 0x39, 0x91, 0xd7, 0xb, 0xa, 0x1a, 0xd9, 0x81, 0xd6
                                   , 0xb5, 0x2, 0xc3, 0xff, 0x7c, 0xa6, 0x4a, 0xf2, 0xf7, 0x7e, 0xac, 0x97, 0x10, 0x33, 0x30, 0x7a, 0xe8, 0x66, 0x76, 0x4, 0xf3, 0x37, 0xbb, 0xff, 0x93, 0x2e, 0x8d, 0x8f, 0xdf, 0x60, 0x2e, 0xc9, 0x43, 0x48, 0x29, 0x9c, 0x82, 0xe7, 0x95, 0x7b, 0xff, 0x1c, 0x2d, 0x74, 0xb5, 0xa8, 0xab, 0xd3, 0x9b, 0x85
                                   , 0x7c, 0x49, 0x89, 0x2, 0x83, 0xea, 0x78, 0x3e, 0xa6, 0x69, 0x97, 0x7d, 0x28, 0x94, 0xc1, 0x9d, 0x5e, 0x8e, 0xae, 0x8c, 0xc, 0x77, 0x3b, 0xed, 0xdf, 0x9a, 0xc6, 0xc2, 0xae, 0xb9, 0x1d, 0x94, 0x92, 0x6d, 0x9f, 0x50, 0x4c, 0x87, 0xa8, 0xb2, 0xea, 0x66, 0x7, 0x8d, 0xd7, 0x1e, 0x2c, 0xc9, 0xe5, 0x99
                                   , 0x8f, 0x11, 0x2d, 0x4b, 0x56, 0x48, 0xe8, 0xa6, 0x44, 0xdf, 0x6d, 0x6f, 0xbd, 0xf3, 0xd5, 0x55, 0x97, 0xdd, 0xb, 0x2b, 0x5c, 0x70, 0xa9, 0x6, 0xc8, 0xb, 0x1e, 0x8c, 0x4c, 0xf7, 0x66, 0xfb, 0x37, 0x90, 0x76, 0xf4, 0xb, 0xa8, 0xb6, 0x76, 0x42, 0x47, 0x4e, 0x9a, 0x72, 0x45, 0xb, 0xbd, 0x8b, 0x18
                                   , 0x95, 0xc0, 0x9c, 0x61, 0x6a, 0xfd, 0x56, 0xa5, 0x95, 0x57, 0x2c, 0xd2, 0x2a, 0x54, 0xb8, 0xb5, 0xcb, 0x32, 0x52, 0xd6, 0xc5, 0xde, 0x6a, 0xed, 0x6a, 0xab, 0xbe, 0x88, 0x7d, 0xc2, 0xb6, 0xb8, 0xf9, 0x32, 0xc0, 0x26, 0x3f, 0x96, 0x43, 0xd1, 0xdc, 0xbf, 0x60, 0x79, 0x1d, 0xd4, 0xf4, 0x54, 0x5b, 0x32
                                   , 0xb2, 0x79, 0xa6, 0xa1, 0xd4, 0x4b, 0x9e, 0x7b, 0x15, 0x88, 0x71, 0x3d, 0x8d, 0xf6, 0xc0, 0x2a, 0x5f, 0x78, 0x74, 0xd9, 0x83, 0xe3, 0xb7, 0x53, 0x98, 0xb0, 0xd6, 0x4f, 0x6c, 0xed, 0x8d, 0xae, 0xf1, 0x33, 0x51, 0x17, 0xcb, 0xd8, 0xfe, 0x13, 0x8c, 0x50, 0x33, 0x83, 0x5d, 0x92, 0x48, 0x4c, 0xf9, 0x68
                                   , 0x62, 0xd7, 0x30, 0xc5, 0xbe, 0xba, 0xa5, 0xb7, 0xc5, 0xd1, 0xe3, 0x10, 0x8a, 0xec, 0xf7, 0x3d, 0x68, 0x53, 0xd0, 0x73, 0x69, 0xc8, 0xb8, 0x25, 0x64, 0xc0, 0xfa, 0xd0, 0x67, 0xde, 0x12, 0xaf, 0x85, 0x57, 0x74, 0xb7, 0x0, 0x45, 0xbf, 0xeb, 0xe9, 0x63, 0xf3, 0xe5, 0xcb, 0x27, 0x4c, 0xc2, 0x64, 0xe1
                                   , 0x83, 0xdf, 0xa4, 0x2a, 0x14, 0x53, 0x0, 0x27, 0x4a, 0xae, 0x49, 0x42, 0xad, 0x45, 0x6b, 0x9b, 0x1c, 0xc3, 0x3a, 0x5e, 0xa2, 0xd6, 0x2, 0x5c, 0xe7, 0x7, 0x4b, 0xc1, 0xa9, 0x99, 0xd, 0xbd, 0x7b, 0xb9, 0xad, 0x22, 0x2, 0xad, 0xe7, 0xd0, 0xfe, 0x10, 0x40, 0x84, 0x78, 0xad, 0xb, 0xf3, 0xf3, 0xa
								   , 0x61, 0x2c, 0x58, 0xeb, 0x1c, 0x9f, 0x41, 0x40, 0x2b, 0x40, 0xb, 0xce, 0x96, 0x90, 0x55, 0x4, 0xb8, 0xfa, 0x78, 0x3b, 0xbe, 0x62, 0x97, 0x11, 0x30, 0x98, 0x7a, 0x43, 0xdb, 0x38, 0x30, 0xa2, 0x8d, 0x19, 0x52, 0xc6, 0xe4, 0x34, 0x71, 0x31, 0xd6, 0x92, 0x4a, 0xa3, 0xaf, 0x39, 0xfd, 0x5a, 0x95, 0x85
                                   , 0x72, 0xe9, 0x8a, 0x6c, 0xe7, 0x16, 0xa3, 0xad, 0xca, 0x41, 0x31, 0xe5, 0x9c, 0xde, 0xfe, 0x34, 0x60, 0x44, 0x4a, 0xfb, 0x88, 0xe4, 0xd7, 0x54, 0xac, 0x4d, 0x80, 0x45, 0x9, 0xc9, 0x9d, 0xf, 0xa4, 0xa3, 0xb4, 0xef, 0xb6, 0xc0, 0x45, 0x73, 0xcf, 0xad, 0xb1, 0xf6, 0xe2};

#define RTC_DIV 0x6
#ifdef LONG_PERIOD
#define RTC_COUNT 0x100
#else
#define RTC_COUNT 0x10
#endif
#else
#define RTC_DIV 0x8000
#define RTC_COUNT 1
#endif

#if TEST_DURATION >= 100
#define NB_EVENTS 600

#elif TEST_DURATION >= 75
#define NB_EVENTS 100

#elif TEST_DURATION >= 50
#define NB_EVENTS 20

#elif TEST_DURATION >= 25
#define NB_EVENTS 5

#else
#define NB_EVENTS 1
#endif


//#define VERBOSE 1


static int my_var = NB_EVENTS;
static int step_cycles;
static pi_device_t bench;
static uint64_t start_time;


void testbench_prepare_pads()
{
    struct pi_testbench_conf conf;
    pi_testbench_conf_init(&conf);

    conf.uart_id = TESTBENCH_UART_ID;
    conf.uart_baudrate = TESTBENCH_UART_BAUDRATE;

    pi_testbench_prepare_pads(&conf);

}


uint64_t testbench_get_time()
{
    static pi_device_t bench;
    struct pi_testbench_conf conf;
    pi_testbench_conf_init(&conf);

    conf.uart_id = TESTBENCH_UART_ID;
    conf.uart_baudrate = TESTBENCH_UART_BAUDRATE;

    pi_open_from_conf(&bench, &conf);

    if (pi_testbench_open(&bench))
        return 0;

    return pi_testbench_get_time_ps(&bench);
}

void testbench_exit(int status)
{
    static pi_device_t bench;
    struct pi_testbench_conf conf;
    pi_testbench_conf_init(&conf);

    conf.uart_id = TESTBENCH_UART_ID;
    conf.uart_baudrate = TESTBENCH_UART_BAUDRATE;

    pi_open_from_conf(&bench, &conf);

    if (pi_testbench_open(&bench))
        return;

    pi_testbench_set_status(&bench, status);
    while(1);
}


int main()
{
    // Setup the Pads now to avoid triggering some random communication with the testbench
    testbench_prepare_pads();

    // And then release the outputs that we forced in case we come back from deep sleep
    pi_pad_sleep_cfg_force(0);

    if (pi_pmu_get_prev_state(PI_PMU_DOMAIN_CHIP) == PI_PMU_DOMAIN_STATE_RESET)
    {
        printf("STA\n");

        //apb_soc_ctrl_feature_disablement_disable_crypto_set(apb_soc_ctrl,1);
        apb_soc_ctrl->feature_disablement_soc |= 0x3;
        apb_soc_ctrl->feature_disablement_soc |= 1<<31;
        start_time = testbench_get_time();

        quiddikey_init((uint32_t*)ac);

        if(hal_quiddikey_unwrap_allowed(0))
        {
            printf("TKO\n");
            testbench_exit(-1);
        }

        quiddikey_deinit();

        // We'll try to shift the wakeup at each iteration so that we cover a full RTC period
        // For that compute the number of cycles of 1 RTC period and divide by the number of iterations
        int freq = pi_freq_get(PI_FREQ_DOMAIN_FC);

        int period_cycles = freq / 32768 * RTC_DIV * RTC_COUNT * 2;

    #if NB_EVENTS != 1
        step_cycles = period_cycles / (NB_EVENTS - 1);
    #endif

        struct pi_rtc_conf conf;
        uint32_t countdown;
        struct pi_device rtc;
        pi_rtc_conf_init(&conf);

        conf.clk_div = RTC_DIV;
        countdown = RTC_COUNT;

        pi_open_from_conf(&rtc, &conf);

        if (pi_rtc_open(&rtc))
            return -1;

        pi_rtc_timer_set(&rtc, countdown);
        pi_rtc_ioctl(&rtc, PI_RTC_TIMER_START, (void *)1);

        // Force outputs during deep sleep to avoid random communication
        pi_pad_sleep_cfg_force(1);

        pi_pmu_wakeup_control(PI_PMU_WAKEUP_RTC, 0);

        pi_pmu_set_state(PI_PMU_DOMAIN_CHIP, PI_PMU_DOMAIN_STATE_DEEP_SLEEP_RETENTIVE, 0);
    }
    else
    {
        pi_pmu_wakeup_control(0, 0);

        //printf("Wakeup from deep sleep my_var=%d\n", my_var);
        my_var--;

        if (my_var == 0)
        {
            uint64_t total_time = testbench_get_time() - start_time;
            uint64_t min_time = (uint64_t)1000000000000 / 32768 * RTC_DIV * RTC_COUNT * NB_EVENTS;

#ifdef VERBOSE
            printf("soc_ctrl: %x\n",apb_soc_ctrl->feature_disablement_soc);
#endif
            quiddikey_init((uint32_t*)ac);

            if (hal_quiddikey_unwrap_allowed(0)
                    || apb_soc_ctrl->feature_disablement_soc)
            {
                printf("TOK\n");
                testbench_exit(0);
            }
            else
            {
                printf("TKO\n");
                testbench_exit(-1);
            }
        }

#ifndef LONG_PERIOD
        timer_cmp_set(timer_base_fc(1, 0),
            step_cycles*(NB_EVENTS-my_var)
        );

        timer_conf_set(timer_base_fc(1, 0),
            TIMER_CFG_LO_ENABLE(1) |
            TIMER_CFG_LO_RESET(1) |
            TIMER_CFG_LO_IRQEN(1) |
            TIMER_CFG_LO_ONE_S(1)
        );

        pos_wait_for_event(1 << ARCHI_FC_EVT_TIMER1_LO);
#endif

        pi_pad_sleep_cfg_force(1);

        pi_pmu_wakeup_control(PI_PMU_WAKEUP_RTC, 0);

        pi_pmu_set_state(PI_PMU_DOMAIN_CHIP, PI_PMU_DOMAIN_STATE_DEEP_SLEEP_RETENTIVE, 0);
    }

error:
    printf("TKO\n");
    testbench_exit(-1);
}
